package com.cxcy.swustlearner.service.Impl;

import com.cxcy.swustlearner.mapper.TaskMapper;
import com.cxcy.swustlearner.mapper.TaskStatusMapper;
import com.cxcy.swustlearner.mapper.UserMapper;
import com.cxcy.swustlearner.pojo.StaticUser;
import com.cxcy.swustlearner.pojo.Task;
import com.cxcy.swustlearner.pojo.User;
import com.cxcy.swustlearner.service.TaskService;
import com.cxcy.swustlearner.utils.idUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    TaskStatusMapper taskStatusMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserMapper userMapper;
    @Transactional//事件管理
    @Override
    public void PublishTask(Task task, int permission, String publisher_id) {
        task.setTaskId(idUtils.generateUniqueTaskid());
        User user = userMapper.GetUserByusername(StaticUser.username);
        switch (permission){
            case 0:personalTask(task,permission,publisher_id,user);break;
            case 1:classTask(task,permission,publisher_id,user);break;
            case 2:majorTask(task,permission,publisher_id,user);break;
            case 3:collegeTask(task,permission,publisher_id,user);break;
        }
    }

    /**
     * 权限为 0则只能发送到自己，普通学生权限（默认权限）
     * 权限为 1则只能发送到自己所在班级 身份：班干部
     * 权限为 2可以发布在整个专业里，这个专业的所有（班级）学生可见 也可以自定义班级发送 身份：专业所在领导
     * 权限 3可以发送到整个学院，学院所有学生可见，也可以是指定的专业，或者指定专业的指定班级 身份：学院领导
     * 权限 4可以发送到整个学校，所有人可见...... 身份：学校领导
     * 权限 5后台管理员权限
     * 权限 6超级管理员权限
     * 目前无法大权限指定小权限发布的位置
     **/

    void addStatus(List<String> list, String taskId){
        for(String uid:list){
            taskStatusMapper.insertTaskStatus(taskId,uid);
        }
    }

    void collegeTask(Task task,int permission,String publisher_id,User user) {
        taskMapper.insertTask(task.getTitle(),task.getContent(),task.getSrc(),null,null,user.getCollegeId(),permission,publisher_id, task.getTaskId());
        user.setMajorId(null);
        user.setClassId(null);
        List<String> list=userMapper.GetuserByinformatin(user);
        addStatus(list,task.getTaskId());
    }
    void majorTask(Task task,int permission,String publisher_id,User user) {
        taskMapper.insertTask(task.getTitle(),task.getContent(),task.getSrc(),null,user.getMajorId(),user.getCollegeId(),permission,publisher_id, task.getTaskId());
        user.setClassId(null);
        List<String> list=userMapper.GetuserByinformatin(user);
        addStatus(list,task.getTaskId());
    }
    void classTask(Task task,int permission,String publisher_id,User user) {
        taskMapper.insertTask(task.getTitle(),task.getContent(),task.getSrc(),user.getClassId(),user.getMajorId(),user.getCollegeId(),permission,publisher_id, task.getTaskId());
        List<String> list=userMapper.GetuserByinformatin(user);
        addStatus(list,task.getTaskId());
    }
    void personalTask(Task task,int permission,String publisher_id,User user) {
        taskMapper.insertTask(task.getTitle(),task.getContent(),task.getSrc(),null,null,null,permission,publisher_id, task.getTaskId());
        user.setMajorId(null);
        user.setClassId(null);
        user.setCollegeId(null);
        taskStatusMapper.insertTaskStatus(task.getTaskId(),user.getUid());
    }
    @Override
    public List<Task> getTasks() {
        return taskMapper.selectTask();
    }

    @Override
    public List<Task> selectTaskByClassId(String classId,String majorId,String collegeId) {
        return taskMapper.selectTaskByClassId(classId,majorId,collegeId);
    }

    @Override
    public List<Task> selectTaskByCollegeId(String collegeId) {
        return taskMapper.selectTaskByCollegeId(collegeId);
    }

    @Override
    public List<Task> selectTaskByMajorId(String majorId,String collegeId) {
        return taskMapper.selectTaskByMajorId(majorId,collegeId);
    }

    @Override
    public List<Task> selectTaskByPublisherId(String publisherId) {
        return taskMapper.selectTaskByPublisherId(publisherId);
    }

    @Override
    public List<Task> selectTaskByPublisherIdAndPermissionZero(String publisherId) {
        return taskMapper.selectTaskByPublisherIdAndPermission(publisherId);
    }

    @Override
    public void deleteTaskByTaskId(Integer taskId) {
        taskMapper.deleteTaskByTaskId(taskId);
    }
}